//
//  SFCDetailTableViewController.m
//  CaiPiao
//
//  Created by apple on 2018/5/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "SFCDetailTableViewController.h"
#import "SFCDetailView.h"

@interface SFCDetailTableViewController ()

@property(nonatomic,retain)NSDictionary* detailDic;

@property (weak, nonatomic) IBOutlet UILabel *IssueNameAndEndTime;
@property (weak, nonatomic) IBOutlet UIView *WinNumberBoxView;

@property (weak, nonatomic) IBOutlet UILabel *SaleMoneyLabel_1;
@property (weak, nonatomic) IBOutlet UILabel *SaleMoneyLabel_2;

@property (weak, nonatomic) IBOutlet UILabel *winCount_1;
@property (weak, nonatomic) IBOutlet UILabel *winCount_2;
@property (weak, nonatomic) IBOutlet UILabel *winCount_3;

@property (weak, nonatomic) IBOutlet UILabel *winMoney_1;
@property (weak, nonatomic) IBOutlet UILabel *winMoney_2;
@property (weak, nonatomic) IBOutlet UILabel *winMoney_3;

@property (weak, nonatomic) IBOutlet UIView *detailBoxView;


@end

@implementation SFCDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    self.title = [NSString stringWithFormat:@"%@开奖详情",[LotteryManager sharedManager].currentLotteryName];
    
    [EasyLodingView showLodingText:@"正在努力加载中..." config:^EasyLodingConfig *{
        return [EasyLodingConfig shared].setLodingType(LodingShowTypeIndicator);
    }];
    
    
    __weak typeof(self)weakSelf = self;
    [[LotteryManager sharedManager] getLotteryDetailWithissueID:[LotteryManager sharedManager].currentDetailID andActionID:@"613" andCallBack:^(BOOL load, NSDictionary *resObj) {
        if (load) {
            weakSelf.detailDic = resObj[@"data"];
            [weakSelf loadDetailAction];
        }
    }];
    
}

-(void)addNumberWith:(NSString*)winNumber{
    CGFloat addSpacing = 5;
    CGFloat ballWith = (self.WinNumberBoxView.frame.size.width-(15*addSpacing))/14;
    
    NSArray* checkArray = [winNumber componentsSeparatedByString:@","];
    if ([checkArray count]>13) {
        for (int i=0; i<[checkArray count]; i++) {
            UIView* numberView = [[UIView alloc] initWithFrame:CGRectMake(i*(ballWith+addSpacing), 10, ballWith, 25)];
            numberView.backgroundColor = [UIColor colorWithRed:(255.0/255.0) green:(90/255.0) blue:0 alpha:1];
            UILabel* numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ballWith, 25)];
            numberLabel.font = [UIFont boldSystemFontOfSize:15];
            numberLabel.textColor = [UIColor whiteColor];
            numberLabel.textAlignment = NSTextAlignmentCenter;
            numberLabel.text = [checkArray objectAtIndex:i];
            [numberView addSubview:numberLabel];
            [self.WinNumberBoxView addSubview:numberView];
        }
    }
}
-(void)loadDetailAction{
    [EasyLodingView hidenLoding];
    NSDictionary* IssueDic = self.detailDic[@"Issue"];
    [self addNumberWith:IssueDic[@"WinNumber"]];
    self.IssueNameAndEndTime.text = [NSString stringWithFormat:@"%@期 %@",IssueDic[@"IssueName"],[IssueDic[@"EndTime"] stringByReplacingOccurrencesOfString:@"/" withString:@"-"]];
    
    NSDictionary* SFCDic = self.detailDic[@"SFC"];
    NSDictionary* RXJDic = self.detailDic[@"RXJ"];
    
    self.SaleMoneyLabel_1.text = [NSString stringWithFormat:@"%@ 元",SFCDic[@"saleAmount"]];
    self.SaleMoneyLabel_2.text = [NSString stringWithFormat:@"%@ 元",RXJDic[@"saleAmount"]];
    
    NSArray* SFCwinDetailArr = SFCDic[@"winDetail"];
    if ([SFCwinDetailArr count]>0) {
        NSDictionary* SFCWinDetailDic_1 = SFCwinDetailArr[0];
        NSDictionary* SFCWinDetailDic_2 = SFCwinDetailArr[1];
        self.winCount_1.text = SFCWinDetailDic_1[@"winCount"];
        self.winCount_2.text = SFCWinDetailDic_2[@"winCount"];
        self.winMoney_1.text = SFCWinDetailDic_1[@"winMoney"];
        self.winMoney_2.text = SFCWinDetailDic_2[@"winMoney"];
    }

    
    NSArray* RXJwinDetailArr = RXJDic[@"winDetail"];
    if ([RXJwinDetailArr count]>0) {
        NSDictionary* RXJWinDetailDic = RXJwinDetailArr[0];
        self.winCount_3.text = RXJWinDetailDic[@"winCount"];
        self.winMoney_3.text = RXJWinDetailDic[@"winMoney"];
    }

    
    NSArray* ItemArray = self.detailDic[@"Item"];
    for (int i=0; i<[ItemArray count]; i++) {
        NSDictionary* ItemDetailDic = ItemArray[i];
        UINib* nib = [UINib nibWithNibName:@"SFCDetailView" bundle:[NSBundle mainBundle]];
        SFCDetailView* view = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];
        view.frame = CGRectMake(0, 30+30*i, self.detailBoxView.frame.size.width, 30);
        view.IssueNoLabel.text = [ItemDetailDic[@"IssueNo"] stringValue];
        view.HTeamLabel.text = ItemDetailDic[@"HTeam"];
        view.ResultLabel.text = ItemDetailDic[@"Result"];
        view.VTeamLabel.text = ItemDetailDic[@"VTeam"];
        [self.detailBoxView addSubview:view];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
//    return 0;
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete implementation, return the number of rows
//    return 0;
//}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
